/*
  目的：common.js的目的就是让项目在浏览器和xcode中都能调试
  约定：1.common是iOS和html共同拥有 在html中凡是要体现到iOS的都要调用common
       2.当使用原生xcode的时候isNative改成true，html的时候使用false
	   3.baseUrl也要随着环境改变
*/
var common = function() {
	let platform = 0;//0ios 1android 2浏览器
	let baseUrl = 'https://test.luyiwangluo.com/api/v15';
	//let baseUrl = 'https://api.luyiwangluo.com/api/v15';
	return {
		//控制台输出
		print:function(e) {
			if (platform == 0) {
				window.webkit.messageHandlers.print.postMessage(e);
			}else if (platform == 1) {
				let msg = {'name':'print','body':e};
				alert(JSON.stringify(msg));//在android中写响应的代码
			}else {
				console.log(e);
			}
		},
		//网络请求
		httpRequest: function(vm, e) {
			if (platform == 0) {
				window.webkit.messageHandlers.httpRequest.postMessage(e);
			}else if (platform == 1) {
				let msg = {'name':'httpRequest','body':e};
				alert(JSON.stringify(msg));//在android中写响应的代码
			} else {
				axios
					.post(baseUrl+e.url, {
						params: e.param,
						headers: {
							'content-type': 'application/x-www-form-urlencoded',
							'apiauth': '',
							'version': 'v1.0.9'
						}
					})
					.then(response => {
						let res = response.data;
						res['url'] = e.url;
						vm.fetchData(res);
					})
					.catch(function(error) { // 请求失败处理
						console.log(error);
					});
			}
		},
		//原生页面跳转
		pushVC:function(e) {
			if (platform == 0) {
				window.webkit.messageHandlers.pushVC.postMessage(e);
			}else if (platform == 1) {
				let msg = {'name':'pushVC','body':e};
				alert(JSON.stringify(msg));//在android中写响应的代码
			} else {
			}
		},
		//html跳转
		pushHtml:function(e) {
			if (platform == 0) {
				window.webkit.messageHandlers.pushHtml.postMessage(e);
			}else if (platform == 1) {
				let msg = {'name':'pushHtml','body':e};
				alert(JSON.stringify(msg));//在android中写响应的代码
			} else {
				window.location.href='../html/'+e+'.html';
			}
		},
        //html跳转
        addNavigationItem:function(e) {
            if (platform == 0) {
                window.webkit.messageHandlers.addNavigationItem.postMessage(e);
            }else if (platform == 1) {
                let msg = {'name':'addNavigationItem','body':e};
                alert(JSON.stringify(msg));//在android中写相应的代码
            } else {
                window.location.href='../html/'+e+'.html';
            }
        }
	}
}();
if (typeof(Vue) == "function") Vue.prototype.common = common;
